//------------- login.js -------------//
$(document).ready(function() {

	//for custom checkboxes
	$('input').not('.noStyle').iCheck({
        checkboxClass: 'icheckbox_flat-green'
    });

	//validate login form 
	$("#login-form").validate({
		ignore: null,
		ignore: 'input[type="hidden"]',
		errorPlacement: function(error, element) {
			wrap = element.parent();
			wrap1 = wrap.parent();
			if (wrap1.hasClass('checkbox')) {
				error.insertAfter(wrap1);
			} else {
				if (element.attr('type')=='file') {
					error.insertAfter(element.next());
				} else {
					error.insertAfter(element);
				}
			}
		}, 
		errorClass: 'help-block',
		rules: {
			email: {
				required: true,
				email: true
			},
			password: {
				required: true,
				minlength: 6
			}
		},
		messages: {
			password: {
				required: "Please provide a password",
				minlength: "Your password must be at least 6 characters long"
			},
			email: "Please type your email",
		},
		highlight: function(element) {
			if ($(element).offsetParent().parent().hasClass('form-group')) {
				$(element).offsetParent().parent().removeClass('has-success').addClass('has-error');
			} else {
				if ($(element).attr('type')=='file') {
					$(element).parent().parent().removeClass('has-success').addClass('has-error');
				}
				$(element).offsetParent().parent().parent().parent().removeClass('has-success').addClass('has-error');
				
			} 
	    },
	    unhighlight: function(element,errorClass) {
	    	if ($(element).offsetParent().parent().hasClass('form-group')) {
	    		$(element).offsetParent().parent().removeClass('has-error').addClass('has-success');
		    	$(element.form).find("label[for=" + element.id + "]").removeClass(errorClass);
	    	} else if ($(element).offsetParent().parent().hasClass('checkbox')) {
	    		$(element).offsetParent().parent().parent().parent().removeClass('has-error').addClass('has-success');
	    		$(element.form).find("label[for=" + element.id + "]").removeClass(errorClass);
	    	} else if ($(element).next().hasClass('bootstrap-filestyle')) {
	    		$(element).parent().parent().removeClass('has-error').addClass('has-success');
	    	}
	    	else {
	    		$(element).offsetParent().parent().parent().removeClass('has-error').addClass('has-success');
	    	}
		},
		submitHandler:function(){
			$.ajax({
				type:"get",
				url:$("#login-form").prop("action"),
				data:{
					usremail:$("#email").val(),
					password:$("#password").val(),
					keeplogin:$("#remember").val()
				},
				success:function(data){
					if(data=='00000'){
						layer.msg("登录成功，欢迎回来！");
						setTimeout(function(){
							location.href="/main";
						},1500);
					}else {
						layer.msg("登录失败：邮箱或密码错误！");
					}
				},
				error:function(err){
					layer.msg("登陆失败："+err.status);
				}
			});
		}
	});
	
	$("#register-form").validate({
		ignore: null,
		ignore: 'input[type="hidden"]',
		errorPlacement: function(error, element) {
			wrap = element.parent();
			wrap1 = wrap.parent();
			if (wrap1.hasClass('checkbox')) {
				error.insertAfter(wrap1);
			} else {
				if (element.attr('type')=='file') {
					error.insertAfter(element.next());
				} else {
					error.insertAfter(element);
				}
			}
		}, 
		errorClass: 'help-block',
		rules:{
			email:{
				required:true,
				email:true,
				remote:{
					url:"/isExist/1",
					type:"post"
				}
			},
			username:{
				required:true,
				minlength:2,
				maxlength:12,
				remote:{
					url:"/isExist/2",
					type:"post"
				}
			},
			password:{
				required:true,
				minlength:6
			},
			confirm_passowrd:{
				required:true,
				minlength:6,
				equalTo:"#password1"
			}
		},
		messages: {
			password: {
				required: "Please provide a password",
				minlength: "Your password must be at least 6 characters long"
			},
			confirm_passowrd:{
				required: "Please provide a password",
				minlength: "Your password must be at least 6 characters long",
				equalTo: "Please provide the same password"
			},
			email: {
				required: "Please provide a email",
				email: "Please type your email",
				remote: "The email has been registered.Please provide a new one."
			},
			username:{
				required: "Please provide a username",
				minlength: "Your username must be at least 2 characters long",
				maxlength: "Your username must be at most 12 characters long",
				remote: "The username has been registered.Please provide a new one. "
			},
		},
		highlight: function(element) {
			if ($(element).offsetParent().parent().hasClass('form-group')) {
				$(element).offsetParent().parent().removeClass('has-success').addClass('has-error');
			} else {
				if ($(element).attr('type')=='file') {
					$(element).parent().parent().removeClass('has-success').addClass('has-error');
				}
				$(element).offsetParent().parent().parent().parent().removeClass('has-success').addClass('has-error');
				
			}
	    },
	    unhighlight: function(element,errorClass) {
	    	if ($(element).offsetParent().parent().hasClass('form-group')) {
	    		$(element).offsetParent().parent().removeClass('has-error').addClass('has-success');
		    	$(element.form).find("label[for=" + element.id + "]").removeClass(errorClass);
		    	$(element.form).find("label[for=" + element.id + "]").remove();
	    	} else if ($(element).offsetParent().parent().hasClass('checkbox')) {
	    		$(element).offsetParent().parent().parent().parent().removeClass('has-error').addClass('has-success');
	    		$(element.form).find("label[for=" + element.id + "]").removeClass(errorClass);
	    	} else if ($(element).next().hasClass('bootstrap-filestyle')) {
	    		$(element).parent().parent().removeClass('has-error').addClass('has-success');
	    	}
	    	else {
	    		$(element).offsetParent().parent().parent().removeClass('has-error').addClass('has-success');
	    	}
		},
		submitHandler:function(){
			$.ajax({
				type:"get",
				url:$("#register-form").prop("action"),
				data:{
					usremail:$("#email1").val(),
					usrname:$("#username").val(),
					password:$("#password1").val(),
				},
				success:function(data){
					if(data=='00000'){
						layer.msg("注册成功！");
						setTimeout(function(){
							location.reload();
						},1200);
					}else{
						layer.msg("注册失败！");
					}
				},
				error:function(err){
					layer.msg("注册失败："+err.status);
				}
			});
		}
	});
	
	$("#remember").on('ifChanged',function(){
		if($(this).prop("checked")){
			$(this).val('1');
		}else{
			$(this).val('0');
		}
	});
});
